const app = getApp();
var util = require('../../utils/util.js');
var amapFile = require('../../libs/amap-wx.js');
Page({
    data: {
        cities: [],
        citiesName: [],
        counties: [],
        area: [],
        navRightItems: [],
        curNav: 1,
        curIndex: 0,
        locationData: true,
        locationCity: '',
        equipmentId: ''

    },
    onLoad: function (option) {
        this.getCities();
        this.getLocation();
        this.setData({
            equipmentId: option.id
        })

    },
    onShow: function () {
        this.getLocation();
        var that = this;
        wx.getSetting({
            success: (res) => {
                if (!res.authSetting['scope.userLocation']) {
                    that.setData({
                        locationData: false
                    })
                } else if (res.authSetting['scope.userLocation']) {
                    that.setData({
                        locationData: true
                    })
                }

            }
        })

    },
    showInput: function () {
        this.setData({
            inputShowed: true
        });
    },
    hideInput: function () {
        this.setData({
            inputVal: "",
            inputShowed: false,
            isAdd: false,
        });
    },
    clearInput: function () {
        this.setData({
            inputVal: ""
        });
    },
    inputTyping: function (e) {

        this.setData({
            inputVal: e.detail.value,
        })
        // this.getUserList(this.data.inputVal);
        // this.getAreaList(this.data.longitude,this.data.latitude,this.data.inputVal)

    },
    getLocation: function () {
        wx.showLoading({
            title: '加载中'
        })
        var that = this;
        var myAmapFun = new amapFile.AMapWX({key: app.globalData.aMapKey});
        wx.getLocation({
            type: 'gcj02', //返回可以用于wx.openLocation的经纬度
            success(res) {
                console.log(res)
                const latitude = res.latitude
                const longitude = res.longitude
                myAmapFun.getRegeo({
                    success: function (data) {
                        wx.hideLoading();
                        //成功回调
                        var city = data[0].regeocodeData.addressComponent.city;
                        console.log(city)
                        that.setData({
                            locationCity: city
                        });
                        for (var item of that.data.cities) {
                            if (item.name == city) {
                                that.getCounty(item.id);
                            }
                        }
                    },
                    fail: function (info) {
                        wx.hideLoading();
                        //失败回调
                        console.log(info)
                        //默认调取郑州数据
                        that.getCounty(174);
                    }
                })
            },
            fail(res) {
                wx.hideLoading();
                console.log(res)
            }
        })
        wx.getSetting({
            success: (res) => {
                if (!res.authSetting['scope.userLocation']) {
                    that.setData({
                        locationData: false
                    })
                } else if (res.authSetting['scope.userLocation']) {
                    that.setData({
                        locationData: true
                    })
                }

            }
        })
    },
    getCities: function () {

        var that = this;
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/active/getCity',
            method: 'GET',
            data: {
                token: app.globalData.token,
            },
            header: {
                'Accept': 'application/json'
            },
            success: function (res) {
                wx.hideLoading()
                console.log(res)
                console.log(res.data)
                if (app.requestEro(res.data.success, res.data.msg)) {
                    var citiesName = res.data.cities.map(function (item) {
                        return item.name;
                    })
                    console.log(citiesName)
                    that.setData({
                        cities: res.data.cities,
                        citiesName: citiesName
                    })
                }
            }
        })
    },
    getCounty: function (city) {
        console.log(city)
        wx.showLoading({
            title: '加载中'
        })
        var that = this
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/active/getCounty',
            method: 'GET',
            data: {
                token: app.globalData.token,
                city: city,
            },
            header: {
                'Accept': 'application/json'
            },
            success: function (res) {
                wx.hideLoading();
                console.log(res.data)
                if (app.requestEro(res.data.success, res.data.msg)) {
                    that.setData({
                        counties: res.data.counties
                    })
                }
            }
        })
    },
    switchCity: function (e) {
        var index = e.detail.value;
        var city = this.data.cities[index];
        console.log(city.id)
        this.setData({
            locationCity: city.name
        })
        this.getCounty(city.id);

    },
    switchRightTab: function (e) {
        let id = e.target.dataset.id,
            index = parseInt(e.target.dataset.index);
        this.setData({
            curNav: id,
        })
        this.getArea(id);

    },
    getArea: function (id) {
        wx.showLoading({
            title: '加载中'
        })
        var that = this
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/active/getArea',
            method: 'GET',
            data: {
                token: app.globalData.token,
                county: id,
            },
            header: {
                'Accept': 'application/json'
            },
            success: function (res) {
                wx.hideLoading();
                console.log(res.data)
                if (app.requestEro(res.data.success, res.data.msg)) {
                    that.setData({
                        area: res.data.areas
                    })
                }
            }
        })
    },
    selectArea: function (e) {
        wx.showLoading({
            title: '加载中'
        })
        var areaId = e.target.dataset.id;
        var areaName = e.target.dataset.name;
        var equipmentId = this.data.equipmentId;
        console.log(areaName)

        var that = this
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/active/saveEquipArea',
            method: 'GET',
            data: {
                token: app.globalData.token,
                equipment_id: equipmentId,
                equipment_area: areaId
            },
            header: {
                'Accept': 'application/json'
            },
            success: function (res) {
                wx.hideLoading();
                console.log(res.data)
                if (app.requestEro(res.data.success, res.data.msg)) {
                    let pages = getCurrentPages();
                    let prevPage = pages[pages.length - 2];
                    console.log(areaName)
                    prevPage.setData({
                        selectedArea: areaName
                    });
                    wx.navigateBack({
                        delta: 1
                    })
                }
            }
        })
    },
    addArea: function () {
        wx.navigateTo({
            url: '/pages/area-add/area-add'
        })
    }


})